﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using BusLib;

namespace GTFSLoader
{
    class GTFSLoader
    {
        static bool DropDb = false;

        delegate object ProcessLine(string line);
        static void ProcessGTFSFile(string filename, ProcessLine lineFunc)
        {
            StreamReader f = new StreamReader(filename);
            int lineCount = 0;

            while (!f.EndOfStream)
            {
                lineFunc(f.ReadLine());
                if (++lineCount % 100 == 0)
                {
                    System.Console.WriteLine(filename + ": " + lineCount.ToString() + " entries loaded.");
                }
            }
            System.Console.WriteLine(filename + ": Done.  " + lineCount.ToString() + " entries loaded.");
        }

        static void Main(string[] args)
        {
            BusLibDataContext db = null;
            if (DropDb)
            {
                db = new BusLibDataContext("Data Source=JEFFDUB-LT;Integrated Security=True");
                db.Connection.Open();

                // Drop & Create the database
                BusLibDataContext.db.Deploy();
                db = BusLibDataContext.db;
            }
            else
            {
                db = BusLibDataContext.db;
                db.Connection.Open();
                System.Data.Common.DbCommand cmd = db.Connection.CreateCommand();

                cmd.CommandText = "truncate table pattern_pair"; cmd.ExecuteNonQuery();
                cmd.CommandText = "truncate table stop_time"; cmd.ExecuteNonQuery();

                cmd.CommandText = "delete from stop"; cmd.ExecuteNonQuery();
                cmd.CommandText = "delete from trip"; cmd.ExecuteNonQuery();
                cmd.CommandText = "truncate table shape_point"; cmd.ExecuteNonQuery();
                cmd.CommandText = "delete from Shape"; cmd.ExecuteNonQuery();
                cmd.CommandText = "delete from Route"; cmd.ExecuteNonQuery();
                cmd.CommandText = "delete from fare_rule"; cmd.ExecuteNonQuery();
                cmd.CommandText = "delete from fare_attribute"; cmd.ExecuteNonQuery();
                cmd.CommandText = "truncate table calendar_date"; cmd.ExecuteNonQuery();
                cmd.CommandText = "delete from Calendar"; cmd.ExecuteNonQuery();
                cmd.CommandText = "delete from Agency"; cmd.ExecuteNonQuery();

            }
            // Load each of the data-files:            
            ProcessGTFSFile("agency.txt", Agency.ImportCSV);
            ProcessGTFSFile("calendar.txt", Calendar.ImportCSV);
            ProcessGTFSFile("calendar_dates.txt", Calendar_Date.ImportCSV);
            ProcessGTFSFile("fare_attributes.txt", Fare_Attribute.ImportCSV);
            ProcessGTFSFile("fare_rules.txt", Fare_Rule.ImportCSV);
            ProcessGTFSFile("routes.txt", Route.ImportCSV);
            ProcessGTFSFile("shapes.txt", Shape_Point.ImportCSV);
            ProcessGTFSFile("trips.txt", Trip.ImportCSV);
            ProcessGTFSFile("stops.txt", Stop.ImportCSV);
            ProcessGTFSFile("stop_times.txt", Stop_Time.ImportCSV);
            ProcessGTFSFile("pattern_pair.txt", Pattern_Pair.ImportCSV);

            db.SubmitChanges();

             int done = 0;
            foreach (Shape s in db.Shapes)
            {
                s.ComputeDistances();
                if ((done++ % 100) == 0)
                {
                    System.Console.WriteLine("Computed shape distances for " + done.ToString() + " / " + db.Shapes.Count());
                }
            }

            done = 0;
            foreach (Trip t in db.Trips)
            {
                t.ComputeStopDistances();
                if ((++done % 100) == 0)
                {
                    System.Console.WriteLine("Computed stop distances for " + done.ToString() + " / " + db.Trips.Count());
                }
            }
        }
    }
}
